package com.example.openapp;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
	private static final String TAG = MainActivity.class.getName();
	private Button addButton;
    private TextView resultField;
    private TextView argumentAField;
    private TextView argumentBField;
	static {  
        System.loadLibrary("ndk1");  
    }  
	private native int myAdder(int a,int b);
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		resultField = (TextView)findViewById(R.id.resultField);
		argumentAField = (TextView)findViewById(R.id.argumentA);
		argumentBField = (TextView)findViewById(R.id.argumentB);
		addButton = (Button)findViewById(R.id.addButton);
		addButton.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
			    doAddition();		
			}
		});
	}
	
	private void doAddition(){
		int a = 0;
		int b = 0;
		int result = 0;
		try{
			a = Integer.valueOf(argumentAField.getText()+"");
			b = Integer.valueOf(argumentBField.getText()+"");
			result = myAdder(a, b);
			resultField.setText("Ergebnis: "+result);
		}catch(Exception e){
			Log.e(TAG,e.getLocalizedMessage());
		}		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}
